<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>epg测试页面</title>
    <script src="js/jquery.min.js" type="text/javascript"></script>

<body>
<div id="java">
    <div style="background-color: greenyellow">正文</div>
    <div id="builderDiv">

    </div>
</div>

</body>
<script type="text/javascript">
    var ticketLog = {package: "com.iqiyi.pps.epg.core.model.ticket", name: "TicketLog", params: [
        {name: "id", type: "long", primaryKey: true},
        {name: "dataId", type: "int"},
        {name: "type", type: "int"},
        {name: "descr", type: "String"},
        {name: "user", type: "String"},
        {name: "addTime", type: "Timestamp"}
    ]}
    var ticketData = {package: "com.iqiyi.pps.epg.core.model.ticket", name: "TicketData", params: [
        {name: "id", type: "long", primaryKey: true},
        {name: "entityId", type: "long"},
        {name: "dataType", type: "int"},
        {name: "keyWord", type: "String"},
        {name: "title", type: "String"},
        {name: "priority", type: "int"},
        {name: "status", type: "int"},
        {name: "reporter", type: "String"},
        {name: "executor", type: "String"},
        {name: "detail", type: "String"},
        {name: "version", type: "int"},
        {name: "addTime", type: "Timestamp"},
        {name: "updateTime", type: "Timestamp", updateTimeKey: true}
    ]}
    var ticket_received_history = { name: "TicketReceivedHistory", params: [
        {name: "id", type: "long", primaryKey: true},
        {name: "dataId", type: "int"},
        {name: "dataType", type: "int"},
        {name: "detail", type: "String"},
        {name: "addTime", type: "Timestamp"}
    ]}

    Util = {};
    Util.decodeDBName = function (name) {
        var nameArr = name.split("");
        var classNameArr = [];
        var tempArr = [];
        nameArr.forEach(function (element, index) {
            var chatCode = element.charCodeAt(0);
            if (chatCode >= 65 && chatCode <= 90) {
                if (tempArr.length > 0) {
                    classNameArr.push(tempArr.join(""));
                    tempArr = [];
                }
                tempArr.push(element.toLowerCase())
            } else {
                tempArr.push(element)
            }
        })
        if (tempArr.length > 0) {
            classNameArr.push(tempArr.join(""));
        }
        return classNameArr.join("_")
    }
    Util.decodeParamMethod = function (elementObj) {
        var htmlArr = [];
        if (elementObj.primaryKey) {
            htmlArr.push("@Id");
            htmlArr.push("@GeneratedValue(strategy = GenerationType.AUTO)");
        }  else if (elementObj.updateTimeKey) {
            htmlArr.push("@Column(name = \"" + Util.decodeDBName(elementObj.name) + "\",insertable = false, updatable = false )");
        }else {
            htmlArr.push("@Column(name = \"" + Util.decodeDBName(elementObj.name) + "\")");
        }
        htmlArr.push("public " + elementObj.type + " get" + Util.firstUppercase(elementObj.name) + "(){return this." + elementObj.name + ";}");
        htmlArr.push("public void set" + Util.firstUppercase(elementObj.name) + "(" + elementObj.type + " " + elementObj.name + "){ this." + elementObj.name + "=" + elementObj.name + " ;}");
        return htmlArr;
    }
    Util.decodeParam = function (elementObj) {
        return "private " + elementObj.type + " " + elementObj.name + ";";
    }
    Util.firstUppercase = function (alphaBert) {
        return  alphaBert.substring(0, 1).toUpperCase() + alphaBert.substring(1);
    }

    Builder = {};
    Builder.buildJava = function (entity) {
        var htmlArr = [];
    //    htmlArr.push("package " + entity.package + ";");
        htmlArr.push("import org.hibernate.annotations.Cache;");
        htmlArr.push("import org.hibernate.annotations.CacheConcurrencyStrategy;");
        htmlArr.push("import javax.persistence.*;");
        htmlArr.push("import java.sql.Timestamp;");
        htmlArr.push("@Entity");
        htmlArr.push("@Table(name = \"" + Util.decodeDBName(entity.name) + "\")");
        htmlArr.push("@Cache(usage = CacheConcurrencyStrategy.NONE)");
        htmlArr.push(" public class " + entity.name + " {");
        entity.params.forEach(function (elementObj, index) {
            htmlArr.push(Util.decodeParam(elementObj))

        })
        entity.params.forEach(function (elementObj, index) {
            htmlArr = htmlArr.concat(Util.decodeParamMethod(elementObj))
        })
        htmlArr.push(" }");
        $("#builderDiv").html(htmlArr.join("<br>"));
    }

</script>
</html>